// src/mixins/paginationMixin.js
export default {
    data() {
      return {
        currentPage: 1,
        itemsPerPage: 10,
        userItemsPerPage: 10, // 新增属性，用于用户设置每页显示的条数
        userInputPage: '' // 新增属性，用于用户输入的跳转页码
      };
    },
    methods: {
      getCurrentPageData(data, currentPage, itemsPerPage) {
        const start = (currentPage - 1) * itemsPerPage;
        const end = start + parseInt(itemsPerPage,10);

        return data.slice(start, end);
      },
      firstPage() {
        this.currentPage = 1;
        this.updateCurrentData();
      },
      prevPage() {
        if (this.currentPage > 1) {
          this.currentPage--;
          this.updateCurrentData();
        }
      },
      nextPage() {
        if (this.currentPage < this.totalPages) {
          this.currentPage++;
          this.updateCurrentData();
        }
      },
      lastPage() {
        this.currentPage = this.totalPages;
        this.updateCurrentData();
      },
      updateCurrentData() {
        this.itemsPerPage = this.userItemsPerPage; // 更新每页显示条数
     
      },
      jumpToPage() {
        const page = parseInt(this.userInputPage);
        if (page > 0 && page <= this.totalPages) {
          this.currentPage = page;
          this.updateCurrentData();
        } else {
          alert('Invalid page number');
        }
      }
    },
    computed: {
        filteredData() {
            if (!this.searchKeyword) {
              return this.data;
            }
            return this.data.filter(item => 
              Object.values(item).some(value => value.toString().includes(this.searchKeyword))
            );
          },
      totalPages() {
        return Math.ceil(this.filteredData.length / this.userItemsPerPage);
      },
      currentData() {
        return this.getCurrentPageData(this.filteredData, this.currentPage, this.userItemsPerPage);
      }
    },
    watch: {
      userItemsPerPage(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.updateCurrentData();
        }
      }
    }
  };
  